﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

public partial class Employee_Rent_ReturnCar : System.Web.UI.Page
{
    DBAcess db = new DBAcess();
    static int bookingID,carID;
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Return Vehicle | Nexus-Z Auto Rental";
        if (Session["employeeID"] == null)
        {
            Response.Redirect("../../Default.aspx");
        }
    }
    protected void go_Click(object sender, EventArgs e)
    {
        String query = "",selectedValue="";
        selectedValue=DropDownList1.SelectedValue;
        if (selectedValue.Equals("License Number"))
        {
            query = "SELECT booking.booking_id, booking.car_id, booking.license_no, car.plate_no, booking.start_date, booking.return_date, car.tariff, insurance.type, insurance.cost, booking.total_amout FROM booking INNER JOIN insurance ON booking.insurance_id = insurance.insurance_id INNER JOIN car ON booking.car_id = car.car_id where booking.status=1 and booking.license_no like '%" + TextBox1.Text + "%';";
        }
        else if (selectedValue.Equals("Plate Number"))
        {
            query = "SELECT booking.booking_id, booking.car_id, booking.license_no, car.plate_no, booking.start_date, booking.return_date, car.tariff, insurance.type, insurance.cost, booking.total_amout FROM booking INNER JOIN insurance ON booking.insurance_id = insurance.insurance_id INNER JOIN car ON booking.car_id = car.car_id where booking.status=1 and car.plate_no= '" + TextBox1.Text + "';";
        }
        else if (selectedValue.Equals("Booking Number"))
        {
            query = "SELECT booking.booking_id, booking.car_id, booking.license_no, car.plate_no, booking.start_date, booking.return_date, car.tariff, insurance.type, insurance.cost, booking.total_amout FROM booking INNER JOIN insurance ON booking.insurance_id = insurance.insurance_id INNER JOIN car ON booking.car_id = car.car_id where booking.status=1 and booking.booking_id='" + int.Parse(TextBox1.Text) + "';";
        }
        else
        {
            query = "SELECT booking.booking_id, booking.car_id, booking.license_no, car.plate_no, booking.start_date, booking.return_date, car.tariff, insurance.type, insurance.cost, booking.total_amout FROM booking INNER JOIN insurance ON booking.insurance_id = insurance.insurance_id INNER JOIN car ON booking.car_id = car.car_id where booking.status=1;";
        }
        SqlDataSource1.SelectCommand = query;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = bookingID.ToString();
        MySqlCommand cmd = new MySqlCommand("update booking set status=0 where booking_id=" + bookingID + ";", db.returnCon());
        db.open();
        cmd.ExecuteNonQuery();
        db.close();
        changeCarAvailability();
        SqlDataSource1.SelectCommand = "SELECT booking.booking_id, booking.car_id, booking.license_no, car.plate_no, booking.start_date, booking.return_date, car.tariff, insurance.type, insurance.cost, booking.total_amout FROM booking INNER JOIN insurance ON booking.insurance_id = insurance.insurance_id INNER JOIN car ON booking.car_id = car.car_id where booking.status=1;";
    }
    protected void changeCarAvailability()
    {
        MySqlCommand cmd = new MySqlCommand("update car set availability=1 where car_id=" + carID + ";", db.returnCon());
        db.open();
        cmd.ExecuteNonQuery();
        db.close();

    }
protected void  GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;
    bookingID = int.Parse(row.Cells[1].Text);
        
    carID = int.Parse(row.Cells[2].Text);
}
}